package com.banzhi.shop.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.banzhi.shop.R;
import com.banzhi.shop.activity.ChoicesDetailsActivity;
import com.banzhi.shop.activity.CommodityDetails_DZZQActivity;
import com.banzhi.shop.data.ChoiceDetailsData;
import com.banzhi.shop.model.CallBack.CommodityDetailsCallBack;
import com.banzhi.shop.model.Impl.ChoiceDetailsImpl;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.ToastUtils;

import org.json.JSONObject;

/**
 * Created by Administrator on 2018/11/10.
 */

public class ChoicePurchaseNotesFrg extends Fragment implements CommodityDetailsCallBack {
    public static final String TAG = "ChoicePurchaseNotesFrg";
    private View view;
    private WebView webView;
  //  private ICommodityDetailsModel model;
    private Activity activity;
    private String GoodID;
    /**
     * 商品详情数据实体类
     */
    private ChoiceDetailsData.GoodsBean goodsdata;
    private ChoiceDetailsImpl model;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.frg_purchase_notes, container, false);
        initView();
     //   model = new CommodityDetailsImpl();
        model = new ChoiceDetailsImpl();
        activity = getActivity();

        if (activity instanceof ChoicesDetailsActivity) {
            GoodID = ((ChoicesDetailsActivity) activity).getGoodID();
        }else if(activity instanceof CommodityDetails_DZZQActivity){
            GoodID = ((CommodityDetails_DZZQActivity) activity).getGoodID();
        }
        this.model.getData(GoodID, "2", this);
        return view;
    }

    @Override
    public void onPause() {
        webView.onPause();
        super.onPause();
    }

    @Override
    public void onDestroy() {
        webView.destroy();
        super.onDestroy();
    }

    private void initView() {
        webView = (WebView) view.findViewById(R.id.webview);
        WebSettings settings = webView.getSettings();
        //自适应屏幕
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        // 支持缩放
        settings.setSupportZoom(true);
        settings.setJavaScriptEnabled(true); // 设置支持javascript脚本
        settings.setTextSize(WebSettings.TextSize.LARGEST);
    }

    /**
     * 展示数据
     */
    private void showWebview() {
        String str = goodsdata.getNotice();
        Log.d("notice", str);
        webView.loadDataWithBaseURL(null, str, "text/html", "UTF-8", null);
    }

    @Override
    public void ongetDataSuccess(Object object) {
        ChoiceDetailsData data = (ChoiceDetailsData) object;
        goodsdata = data.getGoods();

        showWebview();
    }

    @Override
    public void ongetDataError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(getActivity(), jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }
}
